package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.FeedbackDao;
import com.bed.lovewithpatientbed.pojo.Feedback;
import com.bed.lovewithpatientbed.pojo.FeedbackExample;
import com.bed.lovewithpatientbed.service.FeedBackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Tyl
 * @version 1.0.0
 * @ClassName FeedBackServiceImpl.java
 * @Description TODO
 * @createTime 2020年10月20日 14:07:00
 */
@Service
@Transactional
public class FeedBackServiceImpl implements FeedBackService{
    @Resource
    FeedbackDao feedbackDao;
    /**
     * @description 根据条件查所有
     * @author tyl
     * @updateTime 2020/10/20 12:14
     * @param pageBean:分页;startTime:最小时间;maxTime:最大时间
     */
    @Override
    public List<Feedback> selecByExample(LayUiPageBean pageBean, String minTime, String maxTime) throws ParseException {
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        /*添加分页条件*/
        if(pageBean != null && pageBean.getPage() > 0 && pageBean.getLimit() > 0){
            example.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            example.setLimit(pageBean.getLimit().intValue());
        }
        /*日期格式*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date minDate = null;
        Date maxDate = null;
        /*添加日期条件*/
        if(minTime!=null&&!"".equals(minTime)&&maxTime!=null&&!"".equals(maxTime)){
            minDate = format.parse(minTime);
            maxDate = format.parse(maxTime);
            criteria.andFeedbackTimeBetween(minDate,maxDate);
        }
        List<Feedback> feedbackList = feedbackDao.selectByExample(example);
        System.out.println("***************"+feedbackList);
        return feedbackList;
    }
    /*增加*/
    @Override
    public void insertFeedBackMessage(Feedback feedBack) {
        feedbackDao.insert(feedBack);
    }
    /*删除*/
    @Override
    public void deleteFeedBack(Integer feedbackId) {
        feedbackDao.deleteByPrimaryKey(feedbackId);
    }
    /*查一个*/
    @Override
    public Feedback selectOne(Integer feedbackId) {
        return feedbackDao.selectByPrimaryKey(feedbackId);
    }
    /**
     * @description 计算总数
     * @author tyl
     * @updateTime 2020/10/21 11:28
     * @param
     */
    @Override
    public Integer selectCount(LayUiPageBean pageBean, String minTime, String maxTime) throws ParseException {
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        /*添加分页条件*/
        if(pageBean != null && pageBean.getPage() > 0 && pageBean.getLimit() > 0){
            example.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            example.setLimit(pageBean.getLimit().intValue());
        }
        /*日期格式*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date minDate = null;
        Date maxDate = null;
        /*添加日期条件*/
        if(minTime!=null&&!"".equals(minTime)&&maxTime!=null&&!"".equals(maxTime)){
            minDate = format.parse(minTime);
            maxDate = format.parse(maxTime);
            criteria.andFeedbackTimeBetween(minDate,maxDate);
        }
        Long count = feedbackDao.countByExample(example);
        return count.intValue();
    }

}
